
var Order = {
    getItemsPanel:function(baseUrl,id,edit){
        var url= baseUrl + "/order/order/itemspanel/id/"+id;        
        $.ajax({
           type:"POST",
           async:false,
           url:url,
           data: {"edit":edit},
           dataType:"Html",
           success:function(responseText){
             $("#itemsPanel").empty();
             $("#itemsPanel").html(responseText);            
           }
        });
    },
    initPackageDialog:function(){
    	$("#package_dialog").dialog({
            modal: true ,
            height: 400 ,
            autoOpen: false,
            width: 700,
            resizable: true,
            overlay: {
               opacity: 0.6,
               background: "#555555"
            }
    	});
    },
    openPackageDialog:function(baseUrl,orderid){
        var itemsarray=new Array();
   //     var itemid=new Array();
        var items = $("input[name='order_item_chk[]']");
        $.each(items,function(i,obj){
            if($(obj).attr("checked") == true){
                itemsarray.push($(obj).val());                        
                //Order.getPackagePanel(url,id);                                
            }                                 
        });         
        if(itemsarray.length==0){
                 alert("Please select a record.");   
        }else{ 
                itemid=itemsarray.join(",");
                
                Order.getPackagePanel(baseUrl,itemid,orderid);
                
               $("#package_dialog").dialog("open");
               // 
        }       
    },
    
    getPackagePanel:function(baseUrl,id,orderid) {
        var url= baseUrl + "/order/order/subpackage/orderid/"+orderid+"/id/"+id;
        $.ajax({
            type:"POST",
            async:false,
            url:url,
            data:{"orderitemid":id , "orderid":orderid},
            dataType:"Html",
            success:function(responseText){               
                $("#package_dialog").empty();                           
                $("#package_dialog").html(responseText);               
            }
        });    
    },

    addPackage:function(baseUrl, orderid){
        var url= baseUrl + "/order/order/addpackage";
        $.ajax({
            type:"POST",
            //async:false,
            url:url,
            data: $("#packageForm").serialize(),
            dataType:"Html",
            success:function(responseText){
                $("#package_dialog").dialog("close");  
                Order.getItemsPanel(baseUrl, orderid,'true');
            }
        });
    },
    
    delPackage:function(baseUrl, id, orderid){
    	if(confirm('Are you sure you want to do this?')){
	    	var url=baseUrl + "/order/order/delpackage";
	    	$.ajax({
	    		type:"POST",
	    		async:false,
	    		url:url,
	    		data: {"id":id,"edit":true},
	    		dataType:"Html",
	    		success:function(responseText){
	    			Order.getItemsPanel(baseUrl, orderid,'true');
	    		}
	    	});
		}
    },
    
    editPackageDialog:function(baseUrl, id){
    	
    	$("#package_dialog").dialog('destroy');
    	$("#package_dialog").dialog({
            modal: true ,
            height: 400 ,
           // autoOpen: false,
            width: 700,
            resizable: true,
            overlay: {
               opacity: 0.6,
               background: "#555555"
            }
        });
    	Order.editPackagePanel(baseUrl, id);
        //$("#package_dialog").dialog('close');	
    },
    
    editPackagePanel:function(baseUrl, id){
    	var url=baseUrl + "/order/order/editpackage/id/" + id;
    	$.ajax({
    		type:"POST",
    		async:false,
    		url:url,
    		data:{"id":id},
    		dataType:"Html",
    		success:function(responseText){
    			$("#package_dialog").empty();                  
                $("#package_dialog").html(responseText);
    		}
    	});
    },
    
    editPackage:function(baseUrl, orderid){
    	var url=baseUrl + "/order/order/updatepackage";
    	$.ajax({
    		type:"POST",
    		async:false,
    		url:url,
    		data: $("#packageForm").serialize(),
    		dataType:"Html",
    		success:function(responseText){
    			$("#package_dialog").dialog("close");
    			Order.getItemsPanel(baseUrl, orderid,'true');
    		}
    	});
    },
    closebillingaddress:function(){
        if ($("#select").attr("checked")==true){            
            $("#billingaddress").slideUp();
        }else{
            $("#billingaddress").slideDown();
        }
    },
    addnewitems:function(){
        var row="";       
        row+="<div class='form_element_label'>Items ID</div>";
        row+="<div class='form_element'><input type='text' id='itemid' name='itemid[]' value=''/></div>";
        row+="<div class='clear'></div>";
        $("#items").append(row);        
    },
    
    cancelItemDialog:function(baseUrl, id){
    	Order.cancelItemPanel(baseUrl, id);
    	$("#package_dialog").dialog({
            modal: true ,
            height: 400 ,
           // autoOpen: false,
            width: 700,
            resizable: true,
            overlay: {
               opacity: 0.6,
               background: "#555555"
            }
        });
       // $("#showpackagedialog").dialog("open");
    },
    
    cancelItemPanel:function(baseUrl, id){
    	var url = baseUrl + "/order/order/cancelitem/id/" + id;
    	$.ajax({
    		type:"POST",
    		async:false,
    		url:url,
    		data:{"id":id},
    		dataType:"Html",
    		success:function(responseText){
    			$("#package_dialog").empty();                  
                $("#package_dialog").html(responseText);
    		}
    	});
    },
    
    cancelItem:function(baseUrl, id){
    	var url = baseUrl + "/order/order/subcancel/";
    	$.ajax({
    		type:"POST",
    		async:false,
    		url:url,
    		data: $("#cancelForm").serialize(),
    		dataType:"Html",
    		success:function(responseText){
    			$("#package_dialog").dialog("close");
    			Order.getItemsPanel(baseUrl, id,'true');
    		}
    	});
    },
    
    getOrderOperationJournal:function(baseUrl, orderId){
    	var url= baseUrl + "/order/order/operationjournal";        
        $.ajax({
           type:"POST",
           async:false,
           url:url,
           data: {"orderId":orderId},
           dataType:"Html",
           success:function(responseText){
             $("#operation_log_list").empty();
             $("#operation_log_list").html(responseText);            
           }
        });
    },
    
 
    validateJournal : function(value,label){
        $('#'+label+'_error').html('');
        if(ValidationUtil.isBlank(value)){
            var emptyErr = $('#'+label+'Empty').val();
            $('#'+label+'_error').text(emptyErr);
            return false;
        } 
            return true;
    },
    
    journalSubmit : function(baseUrl){
    	var result = true;
    	var operator = $.trim($("#operator").val());
    	var content = $.trim($("#content").val());
    	if(!Order.validateJournal(operator,'operator')){
    		result = false;
    	}
    	if(!Order.validateJournal(content,'content')){
    		result = false;
    	}
    	if(!result){
    		return false;
    	}
    	var orderId = $.trim($('#orderId').val());
    	var orderStatus = $.trim($('#order_status').val())
    	var url = baseUrl + "/order/order/addjournal";
    	$.ajax({
    		type:"POST",
    		async:false,
    		url:url,
    		data: {'orderId':orderId,'orderStatus':orderStatus,'operator':operator,'content':content},
    		dataType:"json",
    		success:function(responseText){
    			$("#operator").val('');
    			$("#content").val('');
    		    Order.getOrderOperationJournal(baseUrl,orderId);
    		}
    	});
    },
    
    deleteJournal : function(baseUrl,journalId){
    	var deleteConfirmInfo = $('#delete_confirm_info').val();
    	if(confirm(deleteConfirmInfo)){
    	    var url = baseUrl + "/order/order/deletejournal";
    	    var orderId = $.trim($('#orderId').val());
    	    $.ajax({
    		    type:"POST",
    		    async:false,
    		    url:url,
    		    data: {'journalId':journalId},
    		    dataType:"json",
    		    success:function(responseText){
    		        Order.getOrderOperationJournal(baseUrl,orderId);
    		    }
    	    });
    	}
    }
}